<?php

$objXajax->registerFunction('frmusersInit');
$objXajax->registerFunction('frmusersLoadPower');
$objXajax->registerFunction('SaveUser');
$objXajax->registerFunction('frmusersLoadPower');

function frmusersInit(){ //xajax_frmusers_Init
    global $objDbSelect,$lang,$userid,$powerid,$powertypeid;
    $objResponse = new xajaxResponse();
    $cbbPowerType = createCbbPowerType($powertypeid,'frmuser_cbbPowerType','xajax_frmusersLoadPower(this.value)');     $objResponse->addAssign('tdPowertype', 'innerHTML',$cbbPowerType);
    return $objResponse->getXML();   
}

/*
* HÃ m dÃ¹ng Ä‘á»ƒ táº¡o chuá»•i combobox cho loáº¡i quyá»?n.
* @param 1 : $powertypeid - Loáº¡i quyá»?n cá»§a user Ä‘Äƒng nháº­p
* @param 2 : tÃªn hÃ m Ä‘ 
*/
function createCbbPowerType($powertypeid,$id,$onChangeFunc='',$all=false){
    global $objDbSelect,$lang;
    $condition = '';
    $powertypeid =1;
    switch($powertypeid){
       case POWER_MASTER : $condition = ' and (id = '.POWER_SUPERVISOR. ' or id = ' .POWER_ACCOUNT. ' or id = ' .POWER_AGENCY.')'; break;
       case POWER_SUPERVISOR : $condition = ' and (id = ' .POWER_ACCOUNT. ' or id = ' .POWER_AGENCY.')'; break;
       case POWER_AGENCY : $condition = 'and (id = '.POWER_CUSTOMER. ' or id = ' .POWER_CUSTOMER_DEMO. ' or id = ' .POWER_MANAGER_CUSTOMER.')'; break;
       default : $condition = ' and 1 = 0'; break;
    }
    $sql =  "select id as id, name_$lang as name from tbl_power_type where active = 1 $condition ";
    //echo $sql; die();
    $arr = $objDbSelect->GetArray($sql);
   // echo '<pre>';print_r($arr);die();
    $strResult = '';
    for($i=0;$i<count($arr);$i++){
        $value = $arr[$i]['id'];
        $name =  $arr[$i]['name'];
        $strResult .= "<option value ='$value'>$name</option>";
    }
    $addFunction ='';    
    if($onChangeFunc!=''){
        $addFunction = "onchange='$onChangeFunc'";
    }
    $firstOption = '';
    if($all){
      $firstOption = "<option value ='' > --Tất cả-- </option> "; 
    }else{
      $firstOption = "<option value ='-1' > --Chọn-- </option> ";   
    }
    $strResult ="<select id='$id' $addFunction>$firstOption".$strResult.'</select>';  
    //echo $strResult;
    return   $strResult;
}


function frmusersLoadPower($powertypeid){ //xajax_frmusers_Init
    global $objDbSelect,$lang;
    $objResponse = new xajaxResponse();
    $cbbPower = createCbbPower($powertypeid,'frmCbbPower'); // láº¥y chuá»•i 
    $objResponse->addAssign('tdPower', 'innerHTML',$cbbPower);
    return $objResponse->getXML();   
}


/*
* HÃ m dÃ¹ng Ä‘á»ƒ táº¡o chuá»•i combobox cho loáº¡i quyá»?n.
* @param 1 : $powertypeid - Loáº¡i quyá»?n duoc truyen vao
* @param 2 : tÃªn hÃ m Ä‘ 
*/
function createCbbPower($powertypeid,$id,$onChangeFunc='',$all=false){
    global $objDbSelect,$lang;
    $condition = '';
    $sql =  "select id as id, name_$lang as name from tbl_power where active = 1 and power_type_id = '$powertypeid'  ";
    $arr = $objDbSelect->GetArray($sql);
    $strResult = '';
    for($i=0;$i<count($arr);$i++){
        $value = $arr[$i]['id'];
        $name =  $arr[$i]['name'];
        $strResult .= "<option value ='$value'>$name</option>";
    }
    $addFunction ='';    
    if($onChangeFunc!=''){
        $addFunction = "onchange='$onChangeFunc'";
    }
    $firstOption = '';
    if($all){
      $firstOption = "<option value ='' > --Tất cả-- </option> "; 
    }else{
      $firstOption = "<option value ='-1' > --Chọn-- </option> ";   
    }
    $strResult ="<select id='$id' $addFunction>$firstOption".$strResult.'</select>';  
    return   $strResult;
}

 
 function SaveUser($arrParams){
    global $objDbUpdate;
    $objResponse = new xajaxResponse();    
    $username       =     addslashes(strtolower($arrParams['txtAddUsername']));
    $pass           =     EncryptPass($arrParams['txtPassword']);
    $power_type_id  =     $arrParams['cmbPowerType'];
    $power_id       =     $arrParams['cbbRoles'];
    $email          =     addslashes($arrParams['txtEmail']);
    $city_id        =     $arrParams['cmbCity'];
    $address        =     addslashes($arrParams['txtAddress']);
    $phone          =     addslashes($arrParams['txtPhone']);
    $active         =     $arrParams['chkActive'];
    
    $sql = "insert into tbl_user(username,pass,power_type_id,power_id,email,city_id,address,phone,active)
    value('$username','$pass',$power_type_id,$power_id,'$email',$city_id,'$address','$phone',$active)";
    if(!$objDbUpdate->Execute($sql)){
        $objResponse->addAlert('Luu that bai');
    }
    $objResponse->addScript('xajax_ajxLoadDataUser();');
    return $objResponse->getXML(); 
 }
 
function ajxLoadDataUser($arrParams=array())
{     
    global $lang,$objDbSelect;
    $power_type = isset($_SESSION['current']['power_type'])?$_SESSION['current']['power_type']:4;
    $lang = isset($_SESSION['lang'])?$_SESSION['lang']:'vi';
    $arrUser = GetUserGrid($arrParams);
    $objResponse = new xajaxResponse();
    $oTbs = new clsTinyButStrong();
    $tmplGrid = 'templates/'.$lang."/main_users_grid.html"; 
    if(!file_exists($tmplGrid)){
      $objResponse->addAlert('Khong co quyen truy cap chuc nang nay');
      $objResponse->addRedirect('index.php?m=home');  
      return $objResponse->getXML();
    }
    $oTbs->LoadTemplate($tmplGrid);
    $oTbs->MergeBlock('blk_data', $arrUser);
    $oTbs->Show(TBS_NOTHING);    
    $objResponse->addAssign('divGrid', 'innerHTML', $oTbs->Source);   
    $objResponse->addScriptCall('initTable();');                              
    return $objResponse->getXML();
}

function  getHotelByAgent(){
    global $objDbSelect;
    $user_id = isset($_SESSION['current']['user_id'])?$_SESSION['current']['user_id']:2;
    $sql ="SELECT id,hotel_name FROM tbl_service where user_create = $user_id";
    $arr = $objDbSelect->GetArray($sql);
    return $arr;
}

function GetUserGrid($arrParams= array()){
    global $objDbSelect;
    $lang = isset($_SESSION['lang'])?$_SESSION['lang']:'vi';
    $condition = '';
    $sql= "select u.id,u.active,username,pt.name_$lang as power_type,p.power_name_$lang as power ,u.address,u.phone,u.email from 
    tbl_user u join tbl_power_type as pt on pt.power_type_id = u.power_type_id join tbl_power  as p on  p.id = u.power_id  ";
    $arr = $objDbSelect->GetArray($sql);
    return $arr;
}

?>
